﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebApi.Bill.Biz;
using WebApi.Models;

namespace WebApi.Controllers
{
    /// <summary>
    /// 接口
    /// </summary>
    public class BufferWorkShopController : BaseController
    {
        #region 获取数据列表
        /// <summary>
        /// GetPageToList
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult<ResponseData> GetPageToList([FromBody] dynamic param)
        {
            ResponseData res = new ResponseData();
            if (!this.Identification(out res))
            {
                return new JsonResult(res);
            }
            try
            {
                int recordCount = 0;
                int totalPage = 0;
                List<BufferWorkShopModel> data = BizBase.Get<BizBufferWorkShop>().GetPageList(param, ref recordCount, ref totalPage);
                if (data != null && data.Count > 0)
                {
                    res.ErrorCode = 0;
                    res.Message = "success";
                    res.DataList = new ModelData
                    {
                        DataList = data,
                        TotalPage = totalPage,
                        RecordCount = recordCount
                    };
                }
                return new JsonResult(res);

            }
            catch (Exception ex)
            {
                return this.ControllerExHandler(res, "GetPageToList", ex);
            }

        }
        #endregion

        #region 根据编码获取单条数据
        /// <summary>
        /// 根据编码获取单条数据
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<BufferWorkShopModel> Get(int code)
        {
            ResponseData res = new ResponseData();
            if (!this.Identification(out res))
            {
                return new JsonResult(res);
            }
            BufferWorkShopModel data = BizBase.Get<BizBufferWorkShop>().Get(code);
            try
            {
                if (data != null)
                {
                    res.ErrorCode = 0;
                    res.Message = "Success";
                    res.DataList = new ModelData
                    {
                        TotalPage = 0,
                        DataList = data
                    };
                }
                else
                {
                    res.ErrorCode = 1;
                    res.Message = "暂无数据";
                    res.DataList = null;
                }

                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                return this.ControllerExHandler(res, "Get", ex);
            }
        }
        #endregion

        #region 编辑数据
        /// <summary>
        /// 编辑数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Edit([FromBody] BufferWorkShopModel model)
        {
            ResponseData res = new ResponseData();
            if (!this.Identification(out res))
            {
                return new JsonResult(res);
            }
            try
            {
                //if (string.IsNullOrEmpty(model.Code))//新增
                //{
                //    BizBase.Get<BizBufferWorkShop>().Add(model);
                //}
                //else//编辑
                //{
                //    BizBase.Get<BizBufferWorkShop>().Update(model);
                //}
                res.ErrorCode = 0;
                res.Message = "success";
                res.DataList = null;
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                return this.ControllerExHandler(res, "Edit", ex);
            }

        }
        #endregion

        #region 删除数据
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="code">广告编码</param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<ResponseData> Delete(int code)
        {
            ResponseData res = new ResponseData();
            if (!this.Identification(out res))
            {
                return new JsonResult(res);
            }
            try
            {
                int result = BizBase.Get<BizBufferWorkShop>().Delete(code);
                if (result == 1)
                {
                    res.ErrorCode = 0;
                    res.Message = "删除成功";
                    res.DataList = new ModelData
                    {
                        TotalPage = 0,
                        DataList = null
                    };
                }
                else
                {
                    res.ErrorCode = 1;
                    res.Message = "删除失败";
                    res.DataList = null;
                }
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                return this.ControllerExHandler(res, "Delete", ex);
            }
        }
        #endregion

        #region 接受车间数据
        /// <summary>
        /// 编辑数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ReceiveWorkShopData([FromBody] dynamic  data)
        {
            ResponseData res = new ResponseData();
            try
            {
                BizBase.Get<BizBufferWorkShop>().ReceiveWorkShopData(data);
                res.ErrorCode = 0;
                res.Message = "success";
                res.DataList = null;
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                return this.ControllerExHandler(res, "ReceiveWorkShopData", ex);
            }

        }
        #endregion

        #region 获取车间数据
        /// <summary>
        /// 编辑数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetWorkShopData()
        {
            ResponseData res = new ResponseData();
            try
            {
                dynamic resData =  BizBase.Get<BizBufferWorkShop>().GetWorkShopData();
                res.ErrorCode = 0;
                res.Message = "success";
                res.DataList = new ModelData {
                    DataList = resData
                };
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                return this.ControllerExHandler(res, "GetWorkShopData", ex);
            }

        }
        #endregion

    }
}
